/****************************************************************************/
/* NAME:           Hal_PwmManagement.c                                      */
/* PURPOSE:        Osmolator management                                     */
/* CREATED_BY:     Vyx                                                      */
/* CREATION_DATE:  09/07/2012                                               */
/************************************** (C) Copyright 2012 AquaLink *********/

#include "Hal_PwmManagement.h"
#include "Hal_PwmManagement_Config.h"
#include "stdio.h"
#include "main.h"

#define PWM_NUMBER       (sizeof(PwmConfigList) / sizeof(PwmConfigList[0]))
#define PWM_BASE_PERIOD  (1000)

/**
  * @brief  Main function of input management.
  *         This function is execute one time at the start.
  * @param  none
  * @retval None
  */
void vTaskCTRL_PwmManagementInit(void)
{
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
    TIM_OCInitTypeDef        TIM_OCInitStructure;
    uint32_t                 Prescaler;
    uint8_t                  i;

    /* Compute frequency              */
    /* f = CPU_clock/prescaler/period */
    /* for f         = 50hz           */
    /*     prescaler = 1440           */
    /*     period    = 1000           */

    /* Time Base configuration */

    for (i=0; i<PWM_NUMBER; i++)
    {
        if (PwmConfigList[i].TimName == TIM_1)
        {
            RCC_APB2PeriphClockCmd( CLOCK_TIM_1, ENABLE);
        }
        if (PwmConfigList[i].TimName == TIM_2)
        {
            RCC_APB1PeriphClockCmd( CLOCK_TIM_2, ENABLE);
        }
        if (PwmConfigList[i].TimName == TIM_3)
        {
            RCC_APB1PeriphClockCmd( CLOCK_TIM_3, ENABLE);
        }
        if (PwmConfigList[i].TimName == TIM_4)
        {
            RCC_APB1PeriphClockCmd( CLOCK_TIM_4, ENABLE);
        }
        if (PwmConfigList[i].TimName == TIM_5)
        {
            RCC_APB1PeriphClockCmd( CLOCK_TIM_5, ENABLE);
        }
        if (PwmConfigList[i].TimName == TIM_6)
        {
            RCC_APB1PeriphClockCmd( CLOCK_TIM_7, ENABLE);
        }
        if (PwmConfigList[i].TimName == TIM_7)
        {
            RCC_APB1PeriphClockCmd( CLOCK_TIM_7, ENABLE);
        }
        if (PwmConfigList[i].TimName == TIM_8)
        {
            RCC_APB2PeriphClockCmd( CLOCK_TIM_8, ENABLE);
        }

        Prescaler = SystemCoreClock / PWM_BASE_PERIOD;
        Prescaler /= PwmConfigList[i].Frequency;

        /* Time Base configuration */
        TIM_TimeBaseStructure.TIM_Prescaler         = Prescaler-1;
        TIM_TimeBaseStructure.TIM_Period            = PWM_BASE_PERIOD-1;
        TIM_TimeBaseStructure.TIM_CounterMode       = TIM_CounterMode_Up;
        TIM_TimeBaseStructure.TIM_ClockDivision     = 0;
        TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
        TIM_TimeBaseInit(PwmConfigList[i].TimName, &TIM_TimeBaseStructure);

        /* Channel 1, 2, Configuration in PWM mode */
        TIM_OCInitStructure.TIM_OCMode      = TIM_OCMode_PWM1;
        TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
        TIM_OCInitStructure.TIM_Pulse       = PwmConfigList[i].DutyCycle;
        TIM_OCInitStructure.TIM_OCPolarity  = TIM_OCPolarity_High;

        switch(PwmConfigList[i].Channel)
        {
            case CHANNEL_1:
                TIM_OC1Init(PwmConfigList[i].TimName, &TIM_OCInitStructure);
                break;
            case CHANNEL_2:
                TIM_OC2Init(PwmConfigList[i].TimName, &TIM_OCInitStructure);
                break;
            case CHANNEL_3:
                TIM_OC3Init(PwmConfigList[i].TimName, &TIM_OCInitStructure);
                break;
            case CHANNEL_4:
                TIM_OC4Init(PwmConfigList[i].TimName, &TIM_OCInitStructure);
                break;
            default:
                break;

        }
        TIM_Cmd(PwmConfigList[i].TimName, ENABLE);
        TIM_CtrlPWMOutputs(PwmConfigList[i].TimName, ENABLE);
    }
}

/**
  * @brief  Get the value of selected input.
  * @param  DutyCycleValue in percent (raw value (0-1000), physical value (0.1-100%), resolution 0.1%)
  * @retval Value of input (TRUE or FALSE)
  */
void PWM_SetPwmValue(uint8_t PwmName, uint16_t DutyCycleValue)
{
  if (PwmConfigList[PwmName].Channel == CHANNEL_1)
  {
    TIM_SetCompare1(PwmConfigList[PwmName].TimName, DutyCycleValue);
  }
  if (PwmConfigList[PwmName].Channel == CHANNEL_2)
  {
    TIM_SetCompare2(PwmConfigList[PwmName].TimName, DutyCycleValue);
  }
  if (PwmConfigList[PwmName].Channel == CHANNEL_3)
  {
    TIM_SetCompare3(PwmConfigList[PwmName].TimName, DutyCycleValue);
  }
  if (PwmConfigList[PwmName].Channel == CHANNEL_4)
  {
    TIM_SetCompare4(PwmConfigList[PwmName].TimName, DutyCycleValue);
  }
}

